
<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
        <el-form-item label="编码:" prop="store_code">
    <el-input v-model="formData.store_code" :clearable="false" placeholder="请输入编码" />
</el-form-item>
        <el-form-item label="名称:" prop="store_name">
    <el-input v-model="formData.store_name" :clearable="false" placeholder="请输入名称" />
</el-form-item>
        <el-form-item label="类型:" prop="store_type">
    <el-input v-model.number="formData.store_type" :clearable="false" placeholder="请输入类型" />
</el-form-item>
        <el-form-item label="执照编号:" prop="business_license">
    <el-input v-model="formData.business_license" :clearable="false" placeholder="请输入执照编号" />
</el-form-item>
        <el-form-item label="执照起始:" prop="license_start_date">
    <el-date-picker v-model="formData.license_start_date" type="date" style="width:100%" placeholder="选择日期" :clearable="false" />
</el-form-item>
        <el-form-item label="执照到期:" prop="license_end_date">
    <el-date-picker v-model="formData.license_end_date" type="date" style="width:100%" placeholder="选择日期" :clearable="false" />
</el-form-item>
        <el-form-item label="药证编号:" prop="pharmacy_license">
    <el-input v-model="formData.pharmacy_license" :clearable="false" placeholder="请输入药证编号" />
</el-form-item>
        <el-form-item label="药证起始:" prop="pharmacy_license_start">
    <el-date-picker v-model="formData.pharmacy_license_start" type="date" style="width:100%" placeholder="选择日期" :clearable="false" />
</el-form-item>
        <el-form-item label="药证到期:" prop="pharmacy_license_end">
    <el-date-picker v-model="formData.pharmacy_license_end" type="date" style="width:100%" placeholder="选择日期" :clearable="false" />
</el-form-item>
        <el-form-item label="省份:" prop="province">
    <el-input v-model="formData.province" :clearable="false" placeholder="请输入省份" />
</el-form-item>
        <el-form-item label="城市:" prop="city">
    <el-input v-model="formData.city" :clearable="false" placeholder="请输入城市" />
</el-form-item>
        <el-form-item label="区县:" prop="district">
    <el-input v-model="formData.district" :clearable="false" placeholder="请输入区县" />
</el-form-item>
        <el-form-item label="地址:" prop="address">
    <el-input v-model="formData.address" :clearable="false" placeholder="请输入地址" />
</el-form-item>
        <el-form-item label="经度:" prop="longitude">
    <el-input-number v-model="formData.longitude" style="width:100%" :precision="2" :clearable="false" />
</el-form-item>
        <el-form-item label="纬度:" prop="latitude">
    <el-input-number v-model="formData.latitude" style="width:100%" :precision="2" :clearable="false" />
</el-form-item>
        <el-form-item label="联系人:" prop="contact_person">
    <el-input v-model="formData.contact_person" :clearable="false" placeholder="请输入联系人" />
</el-form-item>
        <el-form-item label="电话:" prop="contact_phone">
    <el-input v-model="formData.contact_phone" :clearable="false" placeholder="请输入电话" />
</el-form-item>
        <el-form-item label="营业时间:" prop="business_hours">
    <el-input v-model="formData.business_hours" :clearable="false" placeholder="请输入营业时间" />
</el-form-item>
        <el-form-item label="状态:" prop="store_status">
    <el-input v-model.number="formData.store_status" :clearable="false" placeholder="请输入状态" />
</el-form-item>
        <el-form-item label="店长ID:" prop="user_id">
    <el-input v-model.number="formData.user_id" :clearable="false" placeholder="请输入店长ID" />
</el-form-item>
        <el-form-item label="备注:" prop="remark">
    <el-input v-model="formData.remark" :clearable="false" placeholder="请输入备注" />
</el-form-item>
        <el-form-item>
          <el-button :loading="btnLoading" type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {
  createSysStores,
  updateSysStores,
  findSysStores
} from '@/api/system/sysStores'

defineOptions({
    name: 'SysStoresForm'
})

// 自动获取字典
import { getDictFunc } from '@/utils/format'
import { useRoute, useRouter } from "vue-router"
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'


const route = useRoute()
const router = useRouter()

// 提交按钮loading
const btnLoading = ref(false)

const type = ref('')
const formData = ref({
            store_code: '',
            store_name: '',
            store_type: 0,
            business_license: '',
            license_start_date: new Date(),
            license_end_date: new Date(),
            pharmacy_license: '',
            pharmacy_license_start: new Date(),
            pharmacy_license_end: new Date(),
            province: '',
            city: '',
            district: '',
            address: '',
            longitude: 0,
            latitude: 0,
            contact_person: '',
            contact_phone: '',
            business_hours: '',
            store_status: 0,
            user_id: 0,
            remark: '',
        })
// 验证规则
const rule = reactive({
               store_code : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               store_name : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               store_type : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               business_license : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               license_start_date : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               license_end_date : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               pharmacy_license : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               pharmacy_license_start : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               pharmacy_license_end : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               province : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               city : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               district : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               address : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               contact_person : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               contact_phone : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               store_status : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
})

const elFormRef = ref()

// 初始化方法
const init = async () => {
 // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findSysStores({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
}

init()
// 保存按钮
const save = async() => {
      btnLoading.value = true
      elFormRef.value?.validate( async (valid) => {
         if (!valid) return btnLoading.value = false
            let res
           switch (type.value) {
             case 'create':
               res = await createSysStores(formData.value)
               break
             case 'update':
               res = await updateSysStores(formData.value)
               break
             default:
               res = await createSysStores(formData.value)
               break
           }
           btnLoading.value = false
           if (res.code === 0) {
             ElMessage({
               type: 'success',
               message: '创建/更改成功'
             })
           }
       })
}

// 返回按钮
const back = () => {
    router.go(-1)
}

</script>

<style>
</style>
